import requests
import base64
PROMPT = "描述这张图片中的有什么皮肤病？"
PREFIX = "/Users/michael/codes_ai/python/ai_demo_nlp_python/files/"

# 调用 Ollama API
def call_ollama(pic_path,prompt):
    url = "http://localhost:11434/api/chat"
    headers = {"Content-Type": "application/json"}

    # 多模态输入（文本 + 图像）
    payload = {
        "model": "qwen2.5vl:3b",
        "messages": [
            {
                "role": "user",
                "content": prompt, #请描述这张图片的内容：
                "images": [image_to_base64(pic_path)]
            }
        ],
        "stream": False
    }

    response = requests.post(url, json=payload, headers=headers)
    if response.status_code == 200:
        result = response.json()
        print("模型回复:", result["message"]["content"])
        s = result["message"]["content"]
        return s
    else:
        print("请求失败:", response.text)
        return response.text


# 将图片转换为 Base64
def image_to_base64(image_path):
  with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


if __name__ == "__main__":
    call_ollama("%smed001-shi.jpg" % PREFIX,PROMPT)
    call_ollama("%smed002-han.jpg" % PREFIX,PROMPT)
    call_ollama("%smed003-xun.jpg" % PREFIX,PROMPT)
    call_ollama("%smed004-zhi.jpg" % PREFIX,PROMPT)
